package top.superxuqc.autocheckincqupt.checkinconsumer.dao;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import top.superxuqc.autocheckincqupt.checkinconsumer.domain.CQUPTStu;


import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 请填写类的描述
 *
 * @author xuqicheng1
 * @date 2021-08-03 12:27
 */
@Repository
public class DealRedisDao {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public void setKey(String k,String v){
        redisTemplate.opsForValue().set(k,v);
    }

    public void setKey(String k,Object v){
        redisTemplate.opsForValue().set(k, JSONObject.toJSONString(v));
    }
    private final String STU_KEY="stu_key_";
    public void saveSTU(CQUPTStu stu){
        redisTemplate.opsForValue().set(STU_KEY+stu.getXh(),JSONObject.toJSONString(stu));
        saveSTUxh(stu.getXh());
    }

    private final String STU_SET="stu_set";
    private void saveSTUxh(String xh){
        redisTemplate.opsForSet().add(STU_SET,xh);
    }

    public Set<String> getSTUxh(){
        Set<String> xhs = redisTemplate.opsForSet().members(STU_SET);
        return xhs;
    }

    public Set<CQUPTStu> getInfos(){
        Set<String> xhs = getSTUxh();
        Set<CQUPTStu> infos = new HashSet<>();
        for (String xh: xhs
             ) {
            String stu = redisTemplate.opsForValue().get(STU_KEY + xh);
            infos.add(JSONObject.parseObject(stu,CQUPTStu.class));
        }
        return infos;
    }

    public String getValue(String key){
        String s = redisTemplate.opsForValue().get(key);
        return s;
    }
}
